<!--
 * @Description: 图书订单
 * @Author: your name
 * @Date: 2024-06-03
 * @LastEditTime: 2024-06-03
 * @LastEditors: Please set LastEditors
-->

<template>
  <div class="confirmPayment">
    <div class="order_bg">
      <div class="heart_w">
        <div class="order_left">确认订单</div>
        <div class="order_right">
          <ul>
            <li>
              <img src="@/assets/images/order_white.png" alt="" />
              <p class="pactive">订单填写</p>
            </li>
            <li>
              <img src="@/assets/images/line.png" alt="" />
            </li>
            <li>
              <img src="@/assets/images/online_pay.png" alt="" />
              <p>在线支付</p>
            </li>
            <li>
              <img src="@/assets/images/line.png" alt="" />
            </li>
            <li>
              <img src="@/assets/images/order_comp.png" alt="" />
              <p>订单完成</p>
            </li>
          </ul>
        </div>
      </div>
    </div>

    <!-- 商品信息 -->
    <div class="busine_info heart_w">
      <!-- 收货地址 -->
      <div class="shipping_address" v-if="type == 2">
        <h3>选择收货地址</h3>
        <div style="margin-bottom: 10px">
          <ul>
            <li
              class="list_item"
              v-for="(item, index) in addressList"
              :key="item.id"
            >
              <div
                class="item_status"
                :class="
                  active_it == index + 1
                    ? 'active'
                    : item.is_default == 1
                    ? 'active'
                    : ''
                "
                @click="setDefaultAddress(item, index)"
              >
                <p></p>
              </div>
              <p>
                {{ item.name }} &nbsp;&nbsp;&nbsp;{{
                  item.province.name
                }}&nbsp;&nbsp; {{ item.city.name }}&nbsp;&nbsp;
                {{ item.area.name }} {{ item.mobile }}
              </p>
            </li>
          </ul>
        </div>
        <el-button type="danger" size="small" @click="openAddress">
          添加新地址
        </el-button>
      </div>

      <div>
        <h1>商品信息</h1>
        <div class="book">
          <ul>
            <li>
              <el-checkbox v-model="isChooseAll"> 全选 </el-checkbox>
            </li>
            <li style="width: 280px">商品</li>
            <li>单价</li>
            <li>数量</li>
            <li class="subtotal">小计</li>
            <li>操作</li>
          </ul>

          <div class="cart" v-for="item in shoopLists" :key="item.id">
            <div class="checkbox width_t">
              <el-checkbox v-model="item.isChoose"></el-checkbox>
            </div>
            <div class="goods">
              <img :src="item.goods_image" class="bookimg" />
              <h2>{{ item.goods_title }}</h2>
            </div>
            <div class="price width_t">￥{{ item.goods_price }}</div>
            <div class="count width_t">
              <el-input-number
                v-model="item.nums"
                @change="
                  (currentValue, oldValue) => {
                    handleChange(currentValue, oldValue, item)
                  }
                "
                :min="1"
                size="mini"
              ></el-input-number>
            </div>
            <div class="subtotal width_t">{{ item.sumPrice }}.00</div>
            <div class="operation width_t">
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="delListShoops(item)"
              ></el-button>
            </div>
          </div>
        </div>
      </div>

      <!-- 协议 -->
      <div
        id="appAgreement"
        style="
          padding: 0px 30px 0px 39px;
          height: 40px;
          line-height: 40px;
          font-size: 16px;
        "
      >
        <el-checkbox
          v-model="checked"
          @change="getSencond(checked)"
          style="color: #bd0000"
        >
          购买前请阅读融跃教育《用户购买须知》
        </el-checkbox>
        <el-dialog
          :visible.sync="Successdialog"
          width="75%"
          :before-close="sendMsg"
          :show-close="false"
          center
        >
          <span
            style="font-size: 18px; font-weight: 600"
            :class="Sencond !== 0 ? 'span_active' : 'active'"
          >
            阅读 {{ Sencond }} 秒后可关闭页面
          </span>
          <div class="dialog_wrap">
            <div class="dialog_title">《用户购买须知》</div>
            <div class="dialog_content">
              <iframe
                src="https://img.rongyuejiaoyu.com//uploads/20240316/271ebe2fe6dcd98b45d760c14d9837d3.pdf?page=hsn#toolbar=0"
                toolbar="0"
                frameborder="0"
                style="width: 75vw; height: 70vh"
              >
              </iframe>
            </div>
          </div>
          <!-- 底部按钮 -->
          <span slot="footer" class="dialog-footer">
            <el-button
              @click="confirm"
              type="danger"
              :disabled="isDisabled"
              v-if="Sencond === 0"
            >
              我已全部阅读并同意
            </el-button>
          </span>
        </el-dialog>
      </div>

      <ul class="rightinfo">
        <li>
          共1件商品，商品总金额：<span>
            <i class="orMoney" style="font-style: normal; color: #fe6444">
              ￥ {{ total }}
            </i>
          </span>
        </li>
        <li style="display: flex; align-items: center; justify-content: end">
          <button class="submitorder" @click="placeOrder">提交订单</button>
        </li>
      </ul>
    </div>

    <!-- 地址新增 -->
    <DialogCombo ref="dialog"></DialogCombo>
  </div>
</template>

<script>
import DialogCombo from '@/components/common/DialogCombo.vue'
import {
  getAddressAll,
  getListShoop,
  Shoopjian,
  ShoopGetjia,
  Shoopdel
} from '@/api/address'

export default {
  name: 'order_Book',
  components: {
    DialogCombo
  },
  data() {
    return {
      payId: '',
      type: '',
      active_it: 1,
      radioAddress: 1,
      addressList: [], // 收货地址
      shoopLists: [], // 购物车列表
      Sencond: 10,
      coupon_id: 0, // 优惠卷
      isDisabled: false,
      checked: false,
      Successdialog: false //控制弹出
    }
  },
  // 计算属性
  computed: {
    isChooseAll: {
      get() {
        return this.shoopLists.every((item) => item.isChoose)
      },
      set(val) {
        this.shoopLists.forEach((item) => (item.isChoose = val))
      }
    },
    total() {
      return this.shoopLists.reduce((acc, item) => {
        if (item.isChoose) {
          return acc + item.nums * parseFloat(item.goods_price)
        } else {
          return acc
        }
      }, 0)
    }
  },
  created() {
    this.payId = this.$route.query.id
    this.type = this.$route.query.type
    console.log(this.$route.query)
    this.getAddressList()
    this.ShoopList()
  },
  methods: {
    setDefaultAddress(it, i) {
      console.log(it, i)
      this.active_it = i + 1
    },
    placeOrder() {
      if (this.checked) {
        this.$router.push({
          path: '/pay_combo',
          query: {
            id: this.payId,
            coupon_id: this.coupon_id,
            address_id: 29
          }
        })
      } else {
        this.$message.warning({
          message: '请勾选协议!',
          type: 'warning',
          iconClass: ' ', // 这个会覆盖图标类名，为了实现去掉图标的操作
          customClass: 'el-message--warning' // 这个是由于上面设置了iconClass会覆盖掉type，所以需要再设置
        })
      }
    },
    getSencond(val) {
      if (val) {
        this.Successdialog = true
        this.Sencond = 8
        const that = this
        const interval = setInterval(function () {
          --that.Sencond
          if (that.Sencond === 0) {
            that.isDisabled = false
            that.checked = false
            clearInterval(interval)
          }
        }, 1000)
      } else {
        this.checked = false
      }
    },
    getAddressList() {
      getAddressAll().then(({ data: data }) => {
        this.addressList = data.data
      })
    },
    ShoopList() {
      let _that = this
      getListShoop().then(({ data: data }) => {
        // this.shoopLists = data.data
        data.data.map((ele, index) => {
          if (ele.isChoose) {
            ele.isChoose = ele.isChoose
          } else {
            ele.isChoose = false
          }

          if (ele.sumPrice) {
            ele.sumPrice = ele.sumPrice
          } else {
            ele.sumPrice = ele.goods_price * ele.nums
          }
          _that.shoopLists.push(ele)
          return ele
        })
        console.log(data, _that.shoopLists)
      })
    },
    delListShoops(item) {
      this.$confirm('您确定要删除订单吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(async () => {
          let { data: data } = await Shoopdel({ id: item.id })
          this.ShoopList()
          if (data.code == 1) {
            this.$message.success({
              type: 'success',
              message: '删除成功!',
              iconClass: ' ',
              customClass: 'el-message--success'
            })
          }
        })
        .catch(() => {
          this.$message.info({
            type: 'info',
            message: '已取消删除',
            iconClass: ' ',
            customClass: 'el-message--info'
          })
        })
    },
    async handleChange(currentValue, oldValue, item) {
      if (currentValue > oldValue) {
        console.log('增加', currentValue)
        // 小计
        item.sumPrice = item.nums * parseFloat(item.goods_price)
        await ShoopGetjia({ id: item.id })
      } else {
        console.log('减少', currentValue)
        item.sumPrice = item.nums * parseFloat(item.goods_price)
        await Shoopjian({ id: item.id })
      }
    },
    openAddress() {
      this.$refs.dialog.open()
    },
    confirm() {
      if (this.Sencond === 0) {
        this.Successdialog = false
        this.checked = true
      }
    },
    sendMsg() {
      if (this.Sencond === 0) {
        this.Successdialog = false
        this.checked = false
      }
    }
  }
}
</script>

<style scoped lang="scss">
@import '@/assets/styles/order_combo.scss';

.confirmPayment {
  padding-bottom: 35px;
}

#appAgreement {
  ::v-deep {
    .el-checkbox__inner {
      border-color: #bd0000;
    }

    .el-checkbox__input.is-checked .el-checkbox__inner {
      border-color: #409eff !important;
    }
  }
}

.busine_info {
  h2 {
    small {
      padding-left: 48px;
    }
  }

  .book {
    width: 1113px;
    // height: 162px;
    // background: #f3f5f7;
    margin: 20px auto 39px;
    position: relative;

    .subtotal {
      width: 145px;
      color: #cc0000;
      font-weight: bold;
    }

    ul {
      display: flex;
      align-items: center;
      justify-content: space-between;
      line-height: 40px;
      background: #f3f3f3;

      li {
        text-align: center;
        width: 165px;
      }
    }

    .cart:first-child {
      border-bottom: 1px solid #eee;
    }
    .cart {
      margin-bottom: 10px;
      padding: 8px 0;
      height: 110px;
      border: 1px solid #eee;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .price {
        font-weight: 500;
        font-size: 15px;
        color: #ff7d29;
      }
      .width_t {
        width: 165px;
        text-align: center;
      }

      .goods {
        width: 280px;
        text-align: center;
        justify-content: center;
        height: 100%;
        display: flex;
        align-items: center;

        img {
          height: 100%;
        }
        h2 {
          font-size: 16px;
          color: #333;
          font-weight: normal;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
      }
    }
  }

  /* 选择地址 */
  .shipping_address {
    width: 1112px;
    margin: 0 auto;

    h3 {
      line-height: 64px;
      font-size: 18px;
      font-weight: normal;
      color: #666;
    }

    ul {
      border-bottom: 1px solid #e5e5e5;
      padding-bottom: 8px;
    }
    .list_item {
      display: flex;
      align-items: center;
      line-height: 38px;
      position: relative;

      .active {
        p:first-child {
          border: 1px solid #2280f0 !important;
          &::after {
            content: ' ';
            display: block;
            background: #2280f0;
            width: 10px;
            height: 10px;
            text-align: center;
            margin: 3px auto;
            border-radius: 50%;
          }
        }
      }

      .item_status {
        display: inline-block;
        position: relative;
        cursor: pointer;
        font-weight: 500;
        color: #606266;
        margin-right: 6px;

        p:first-child {
          content: ' ';
          display: block;
          border-radius: 50%;
          width: 16px;
          height: 16px;
          border: 1px solid #ccc;
          position: absolute;
          top: -8px;
          left: -25px;
        }
      }

      ::v-deep {
        .el-radio {
          margin-right: 0;
        }
      }
      p {
        margin-left: 23px;

        &:hover {
          background: #fff3f3;
        }
      }
    }
  }
}

.rightinfo {
  text-align: right;
  margin-right: 45px;
  font-size: 18px;

  li {
    height: 40px;
    line-height: 40px;
    color: #999;

    .jine {
      color: #e8433a;
      font-size: 22px;
      font-weight: bold;
      font-style: normal;
    }

    button {
      width: 120px;
      height: 40px;
      background: linear-gradient(-90deg, #fa4e4e, #fd894f);
      border-radius: 6px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 15px;
      color: #fff;
      border: none;
      cursor: pointer;
    }
  }
}

/* 页签样式 */

.dialog_wrap {
  background-color: #fff;
  box-sizing: border-box;
}

.dialog_title {
  text-align: center;
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 25px;
}

.dialog_content {
  overflow-y: auto;
  line-height: 1.5;
}

.span_text {
  cursor: pointer;
  margin: 10px 10px;
  border-radius: 5px;
  display: inline-block;
  width: 80px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  border: 1px solid skyblue;
}

.span_active {
  color: #ccc;
}
</style>
